/*
 * Title: WADS
 * Description: Web-based Auto flow Documents System
 * Copyright: 2007
 * Company: CoE
 * @author: Mr.Nattasan Wang-Aphisit
 * @version 1.1
 */

package wads.drawing.line;

import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.font.TextLayout;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import wads.drawing.node.CaseNode;

public class WADLink extends CaseLink {

  public static final String NAME = "WAD Link";
//  public static final String LINK_CONDITION_TYPE_KEYNAME = "ConditionType";
//  public static final String LINK_CONDITION_EXPRESSION_KEYNAME = "ConditionExpression";
  private static final Font LINK_TEXT_LABEL_FONT = new Font("Courier", Font.PLAIN, 10);

  private int arrowHeight = 8; //AB
  private int arrowWidth = 8; //EC
  
  private String conditionType;
  private String conditionExpression;

  /** 
   *  Constructs the "shape" of the association link about origin.
   *  Note that it does not immediately constructs it at the point
   *  and angle at which the link is placed.
   */

  public WADLink(Point p) {
    super(p.x, p.y, p.x, p.y);
    id = ++idCount;
    setName(NAME);
  }

  public WADLink(int x1, int y1, int x2, int y2){
    super(x1, y1, x2, y2);
    id = ++idCount;
    setName(NAME);
  }

  public WADLink(CaseNode start, CaseNode end) {
      super(start.getStartX(), start.getStartY(), start.getStartX(), start.getStartY());
      setStartNode(start);
      setEndNode(end);
      this.createLink(start, end);
      id = ++idCount;
      setName(NAME);
  }
  
  @Override
  public String toString() {
    return "Link ID: "+getId()
            +", Condition Type: "+getConditionType()
            +", Condition Expression: "+getConditionExpression()
            +", Start Node: "+getStartNode().getName()
            +", End Node: "+getEndNode().getName();
  }
  
  @Override
  public void drawText(Graphics2D g2d, Point midp) {
    String linkText = "("+getId()+") "+this.getConditionType()+": "+this.getConditionExpression();
    TextLayout linkLabel = new TextLayout(linkText, LINK_TEXT_LABEL_FONT, g2d.getFontRenderContext());
    
    if(this.getConditionType()!=null && this.getConditionExpression()!=null) {
      g2d.drawString(linkText, midp.x-(int)(linkLabel.getBounds().getWidth()/2), midp.y-10);
    }
  }

  @Override
  protected void createLink(int x1, int y1, int x2, int y2){
    //Initialize links properties
    super.initLink(x1, y1, x2, y2);

    //Compute length of line
    double dist = getDistance(x1, y1, x2, y2);

    //End X-coord of line
    float lineEndX = (float)dist;

    int dashWidth = 5;
    int dashGap = 5;
    int startX = 0;
    int endX = dashWidth;

    //Constructs dashed line segment that connects with arrow
    /* Note: Currently, the line segment when transformed, becomes
       "crooked". The dash looks rounded. This is a bug in Java 1.2 and
       no workaround is available yet.
    */
    while (endX < lineEndX){
      append(new Line2D.Float(startX, 0, endX, 0));
      startX += (dashWidth + dashGap);
      endX += (dashWidth + dashGap);
    }

    //Constructs an arrow
    GeneralPath arrow = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
    arrow.moveTo(lineEndX-arrowWidth, -arrowHeight/2);   //move to Pt A
    arrow.lineTo(lineEndX, 0);   //draw line to Pt B
    arrow.lineTo(lineEndX-arrowWidth, arrowHeight/2); //draw line to Pt C
    append(arrow);
  }

  @Override
  public boolean needsTransform(){
    return true;
  }
  
//  public Properties getLinkProperties() {
//    return linkProp;
//  }
//
//  public Object setLinkProperties(String propName, String propValue) {
//    System.out.println("Set Properties: Key="+propName+" Value="+propValue);
//    return linkProp.setProperty(propName, propValue);
//  }

    /**
     * @return the ConditionType
     */
    public String getConditionType() {
        return conditionType;
    }

    /**
     * @param ConditionType the ConditionType to set
     */
    public void setConditionType(String ConditionType) {
        this.conditionType = ConditionType;
    }

    /**
     * @return the ConditionExpression
     */
    public String getConditionExpression() {
        return conditionExpression;
    }

    /**
     * @param ConditionExpression the ConditionExpression to set
     */
    public void setConditionExpression(String ConditionExpression) {
        this.conditionExpression = ConditionExpression;
    }
}